home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Simplest Viewer App / OLD / VwrSimplest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  4.1 KB  |  180 lines  |  [TEXT/CWIE]

  1. /*
  2. **    This is the simplest viewer application reasonable
  3. **
  4. **    Nick Thompson, nickt@apple.com
  5. */
  6.  
  7.  
  8. /*------------------------------------------------------------*/
  9.  
  10. /* all we need is this header - to use the viewer */
  11. #include <QD3DViewer.h>
  12.  
  13. /* this header allows us to check for the viewer */
  14. #include <CodeFragments.h>
  15.  
  16. /*------------------------------------------------------------*/
  17.  
  18. /* function prototypes for the application */
  19. Boolean     IsQD3DViewerInstalled( void ) ;
  20. void        MainEventLoop( void ) ;
  21.  
  22.  
  23. /*------------------------------------------------------------*/
  24.  
  25. /* constants */
  26. const    short    kWindHeight = 250 ;
  27. const    short    kWindWidth = 200 ;
  28.  
  29. /*------------------------------------------------------------*/
  30.  
  31. /*
  32. ** this is a simple program, initialise the managers
  33. ** check that the viewer is installed
  34. ** ask for a 3DMF file
  35. ** create a window
  36. ** attach it to a viewer
  37. ** handle events until the window is closed
  38. ** Quit
  39. **
  40. ** No menus, not really an app :-)
  41. */
  42.  
  43. void main(void)
  44. {
  45.  
  46.     short                myNumTypes = 1 ; 
  47.     SFTypeList            myTypeList = { '3DMF' } ;
  48.     StandardFileReply    mySFReply ;
  49.     OSErr                theErr = noErr ;
  50.     short                myRefNum ;
  51.     WindowPtr            myWind = nil ;
  52.     Rect                myRect = { 0, 0, kWindHeight, kWindWidth } ;
  53.     TQ3ViewerObject        myViewer ;
  54.     
  55.     /* Initialize all the needed managers. */
  56.     InitGraf((Ptr)&qd.thePort);
  57.     InitFonts();
  58.     InitWindows();
  59.     InitMenus();
  60.     TEInit();
  61.     InitDialogs((long)nil);
  62.     InitCursor();
  63.     
  64.     /*
  65.     ** must call this so that the heap is expanded to maximum
  66.     ** size before calling any viewer routines
  67.     */
  68.     MaxApplZone() ;
  69.     
  70.     /* we weak linked against the Viewer, let's check that it is installed */
  71.     
  72.     if( (long) Q3ViewerNew != kUnresolvedCFragSymbolAddress ) {
  73.         StandardGetFile( nil, myNumTypes, myTypeList, &mySFReply ) ;
  74.         
  75.         if( mySFReply.sfGood ) {
  76.             theErr = FSpOpenDF( &mySFReply.sfFile,  
  77.                                 fsRdPerm, 
  78.                                 &myRefNum ) ;
  79.                                 
  80.             OffsetRect( &myRect, 50, 50 ) ; 
  81.                               
  82.             myWind = NewCWindow( nil, 
  83.                                  &myRect, 
  84.                                  "\pViewerApp", 
  85.                                  true, 
  86.                                  documentProc, 
  87.                                  (WindowPtr)-1, 
  88.                                  true, 
  89.                                  0L ) ;
  90.                                  
  91.  
  92.             if(myViewer = Q3ViewerNew( (CGrafPtr)myWind, &myWind->portRect, kQ3ViewerDefault )) 
  93.             {
  94.                 /* if the viewer is not nil, we created it ok... */
  95.                 theErr = Q3ViewerUseFile( myViewer, myRefNum );
  96.                 SetWRefCon( myWind, (long)myViewer ) ;        /* stuff the reference in the refcon for later use */
  97.                 MainEventLoop() ;
  98.             }
  99.  
  100.         }
  101.     }
  102.  
  103.     ExitToShell();    
  104.     
  105. }
  106.  
  107.  
  108. /*------------------------------------------------------------*/
  109.  
  110. /*
  111. ** we want to handle events until the fronmost window goes away
  112. */
  113.  
  114. void    MainEventLoop( void ) 
  115. {
  116.     WindowPtr        myWind ;
  117.     Boolean            gotEvent ;
  118.     TQ3ViewerObject    theViewer ;
  119.     OSErr            theErr ;
  120.     RgnHandle        tempRgn ;
  121.     Rect            dragRect ;
  122.     EventRecord        theEvent ;
  123.     GrafPtr            savedPort ;
  124.     
  125.     while(( myWind = FrontWindow()) != nil )
  126.     {
  127.         
  128.         gotEvent = WaitNextEvent( everyEvent, &theEvent, GetCaretTime(), nil ) ;
  129.         if( gotEvent )
  130.         {
  131.             switch( theEvent.what )
  132.             {
  133.               case updateEvt:
  134.                   myWind = (WindowPtr)theEvent.message ;
  135.                 
  136.                 theViewer = (TQ3ViewerObject)GetWRefCon( myWind ) ;
  137.                 BeginUpdate( myWind ) ;
  138.                 theErr = Q3ViewerDraw( theViewer );
  139.                 EndUpdate( myWind ) ;
  140.                 
  141.                 break ;
  142.                 
  143.               case mouseDown:
  144.                 switch( FindWindow( theEvent.where, &myWind ) )
  145.                 {
  146.                   case inGoAway:
  147.                     theViewer = (TQ3ViewerObject)GetWRefCon( myWind ) ;
  148.                       theErr = Q3ViewerDispose( theViewer);
  149.                     DisposeWindow( myWind ) ;
  150.                     break ;
  151.                     
  152.                   case inContent:
  153.                       /*
  154.                       ** There is a bug in versions 1.0.4 and earlier of the Viewer,
  155.                       ** so the port has to be set and restored.
  156.                       */
  157.                     GetPort( &savedPort ) ;
  158.                     SetPort((GrafPtr)myWind ) ;
  159.                 
  160.                     Q3ViewerEvent( theViewer, &theEvent);
  161.                     
  162.                     SetPort( savedPort ) ;
  163.                     break ;
  164.                     
  165.                   case inDrag:
  166.                       tempRgn = GetGrayRgn() ;
  167.                       dragRect = (**tempRgn).rgnBBox ;
  168.                       DragWindow( myWind, theEvent.where, &dragRect ) ;
  169.                     break ;
  170.                 }
  171.                 break ;    
  172.             }
  173.         }
  174.         SetPort(savedPort ) ;
  175.     }    
  176.  
  177. }
  178.  
  179.  
  180.